package com.cwg.dict.ui.rcp;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Set;

import org.eclipse.ui.IViewReference;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import cn.edu.ynu.sei.dict.plugin.stardict.StarDictReader;

import com.cwg.dict.service.DictManager;
import com.cwg.dict.service.NetDictQueryService;
import com.cwg.dict.service.Speaker;
import com.cwg.dict.service.impl.DictManagerImpl;
import com.cwg.dict.service.impl.FileHelper;
import com.cwg.dict.service.impl.HistoricalImpl;
import com.cwg.dict.service.impl.LoadHelper;
import com.cwg.dict.service.impl.SpeakerImpl;
import com.cwg.dict.stem.service.Translator;
import com.cwg.dict.stem.service.WordParser;
import com.cwg.dict.stem.service.impl.TranslatorImpl;
import com.cwg.dict.stem.service.impl.WordParserImpl;

public class Activator extends AbstractUIPlugin {

	// The shared singleton instance.
	private static Activator	plugin;

	public static Activator getDefault() {
		return plugin;
	}

	private DictManagerImpl	dictManager;
	private HistoricalImpl	historical;
	private String[]		netDictNames;
	private SpeakerImpl		speaker;
	private TranslatorImpl	translator;

	private WordParserImpl	wordParser;

	public Activator() {
		plugin = this;
	}

	public IViewReference findViewreference(String id) {
		IViewReference[] refs = getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
		for (IViewReference ref : refs) {
			if (id.equals(ref.getId())) {
				return ref;
			}
		}
		return null;
	}

	public IViewReference findViewreference(String id, String secondaryId) {
		IViewReference[] refs = getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
		for (IViewReference ref : refs) {
			if (id.equals(ref.getId()) && secondaryId.equals(ref.getSecondaryId())) {
				return ref;
			}
		}
		return null;
	}

	public DictManager getDictManager() {
		return dictManager;
	}

	public HistoricalImpl getHistorical() {
		return historical;
	}

	public String[] getNetDictNames() {
		return netDictNames;
	}

	public NetDictQueryService getNetDictQueryService() {
		return dictManager;
	}

	public Speaker getSpeaker() {
		return speaker;
	}

	public Translator getTranslator() {
		return translator;
	}

	public WordParser getWordParser() {
		return wordParser;
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		dictManager = new DictManagerImpl();
		FileHelper fileHelper = new FileHelper();
		fileHelper.setHomeDirectory(new File("C:\\Documents and Settings\\Administrator\\.stem-dic"));
		dictManager.setFileHelper(fileHelper);
		dictManager.setLoadHelper(new LoadHelper());
		Set<String> keySet = dictManager.initDefaultNetDicts().keySet();
		netDictNames = keySet.toArray(new String[keySet.size()]);
		translator = new TranslatorImpl();
		try {
			translator.setStardictReader(new StarDictReader("C:\\Documents and Settings\\Administrator\\.stem-dic\\dicts\\stardict-langdao-ec-big5-2.4.2",
					"langdao-ec-big5"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		wordParser = new WordParserImpl();
		historical = new HistoricalImpl();
		File historyFile = new File(fileHelper.getHomeDirectory(), "history.his");

		if (!historyFile.exists()) {
			historyFile.createNewFile();
		}
		historical.setHistoryFile(historyFile);
		historical.load();
		speaker = new SpeakerImpl();
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
	}

}
